﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;

namespace AdminSite.Controllers
{
    public class AliMaMaController : Controller
    {

        IAliMaMaService IAliMaMa = new AliMaMaService();
        IProductService IProduct = new ProductService();
        IIDCreaterService IIDCreater = new IDCreaterService();
        public ActionResult Index(int productID)
        {
            IList<AliMaMaModel> model = IAliMaMa.GetByProductID(productID);

            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(string productID, string MMID)
        {
            ViewData["ProductID"] = productID;
            if (!string.IsNullOrEmpty(MMID))
            {
                 AliMaMaModel model= IAliMaMa.GetByMMID(int.Parse(MMID));
                 return View(model);
            }
            return View();
        }

        [HttpPost]
        public ActionResult Edit(AliMaMaModel model)
        {
            string retval = "";
            if (model.MMID > 0)
            {
                AliMaMaModel oldModel = IAliMaMa.GetByMMID(model.MMID);
                model.CreateTime = oldModel.CreateTime;
                model.Enable = oldModel.Enable;
                model.UpdateTime = DateTime.Now;
                IAliMaMa.UpdateByMMID(model);
                retval = "修改成功";
            }
            else
            {
                model.MMID = IIDCreater.GetNewID("MMID", "推广编号", 1000);
                model.Enable = false;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Parse("1900-01-01");
                IAliMaMa.AddAliMaMa(model);
                retval = "添加成功";
            }

            IProduct.UpdateTaoKe(model.ProductID, true);
            return Content(retval);
         
        }
        public ActionResult TaoKe(int id)
        {
            IList<AliMaMaModel> model = IAliMaMa.GetByProductID(id);
            ProductModel product = IProduct.GetByProductID(id);

            ViewData["ProductModel"] = product;

            return View(model);
        }


    }
}
